<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <h1>todolist</h1>
    <input type="text" v-model="value" @keyup.enter="add">
    <button @click="add">添加</button>

    <h3>未完成</h3>
    <ul>
      <li v-for="item in unfinishArr" :key="item.id">
        <input type="checkbox" v-model="checkedArr" :value="item.id">
        {{ item.value }}
        <button @click="remove(item.id)">删除</button>
      </li>
    </ul>

    <h3>以完成</h3>
    <ul>
      <li v-for="item in finishArr" :key="item.id">
        <input type="checkbox" v-model="checkedArr" :value="item.id">
        {{ item.value }}
        <button @click="remove(item.id)">删除</button>
      </li>
    </ul>

    <p>checkedArr: {{checkedArr}}</p>
  </div>

  <script src="../Vue.js"></script>

  <script>
    const { createApp } = Vue;

    createApp({
      data() {
        return {
          arr: [{
            value: "zhangsan",
            id: 1,
          }, {
            value: "lisi",
            id: 2,
          }],
          value: "",
          id: 3,
          checkedArr: []
        }
      },
      computed: {
        unfinishArr() {
          return this.arr.filter(item => !this.checkedArr.includes(item.id))
        },
        finishArr() {
          return this.arr.filter(item => this.checkedArr.includes(item.id))
        }
      },
      methods: {
        add() {
          // console.log(this.value)
          this.arr.push({
            id: this.id,
            value: this.value,
          })
          this.id++;
          this.value = "";
        },
        remove(id) {
          console.log(id);
          this.arr = this.arr.filter(item => {
            return item.id !== id
          })
        }
      }
    }).mount("#app")

  </script>
</body>
</html>